let uEmailInp = document.querySelector("#useremail")
let uEmailText = document.querySelector(".emailtext")
let codeInp = document.querySelector("#code")
let codeText = document.querySelector(".codetext")
let passwordInp = document.querySelector("#password")
let passwordText = document.querySelector(".passtext")
let overBtn=document.querySelector(".over")
//输入框聚焦失焦的效果
uEmailInp.addEventListener("blur", uEmailInpBlur)
uEmailInp.addEventListener("focus", uEmailInpFocus)
codeInp.addEventListener("blur", codeInpBlur)
codeInp.addEventListener("focus", codeInpFocus)
passwordInp.addEventListener("blur", wordInpBlur)
passwordInp.addEventListener("focus", wordInpFocus)
overBtn.addEventListener("click",overChange)
//点击，请求验证码+倒计时 且本getCode已在公共js里声明
getCode.addEventListener("click", async function (e) {
    if (judgeEmail() == false){
        judgeRemoveTime()
        return} ;
    getCode.style.color = "#838f8f"
    getCode.disabled = true;
    Countdown(getCode.textContent, 30, getCode, function () {
        getCode.disabled = false
        getCode.style.color = "blue"
    });
    // 发起请求。获取验证码
    let res = await axios({
        method: 'post',
        url: 'http://121.37.252.168:8080/api/user/updatePasswordEmail',
        headers: {
            'Content-Type': 'application/json;charset=utf8'
        },
        data: {
            email: uEmailInp.value
        }
    })

})
//聚焦动画
function uEmailInpFocus(e) {
    let inp=e.target
    inp.placeholder = ""
    uEmailText.classList.add("inputclick")

}

function uEmailInpBlur(e) {
    let inp=e.target
    if (inp.value == "") {
        inp.placeholder = "邮箱"
    }
    if (inp.value == "" && inp.placeholder== "邮箱") {

        uEmailText.classList.remove("inputclick")
    }
}
function codeInpFocus(e) {
    let inp=e.target
    inp.placeholder = ""
    codeText.classList.add("inputclick")
}
function codeInpBlur(e) {
    let inp=e.target
    if (inp.value == "") {
        inp.placeholder = '请输入验证码'
    }
    if (inp.value == "" && inp.placeholder== '请输入验证码') {

        codeText.classList.remove("inputclick")
    }
}
function wordInpFocus(e) {
    let inp=e.target
    inp.placeholder = ""
   passwordText.classList.add("inputclick")

}

function wordInpBlur(e) {
    let inp=e.target
    if (inp.value == "") {
        inp.placeholder = "新密码(8-20位)特殊字符可选(,._!@#$^&*)"
    }
    if (inp.value == "" && inp.placeholder== "新密码(8-20位)特殊字符可选(,._!@#$^&*)") {
        passwordText.classList.remove("inputclick")
    }
}



// 完成修改，验证验证码是否正确
async function overChange(){
    if (!judgeEmail()) {   
        judgeRemoveTime()
    } else if (!judgePassWord()){
        judgeRemoveTime()
    }
    else{
        let res = await axios({
            method: 'post',
            url: 'http://121.37.252.168:8080/api/user/updatePassword',
            headers: { 
               'Content-Type': 'application/json'
            },
            data : {    
                    email: uEmailInp.value,
                    emailCode: codeInp.value,
                    password: passwordInp.value, 
            }
        })
        if(res.data.code!=200){
            judgeCode(res.data.msg)
            judgeRemoveTime()
            return
        }
         // 验证码正确跳转下个页面
         else{
            tipsEl.textContent="修改成功,2s后自动跳转"
            tipsEl.classList.add("active")
            judgeRemoveTime()
            window.location.href="../public/login.html"
        }
    
    }
}
